package us.codecraft.webmagic.pipeline;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.model.BaseDO;
import us.codecraft.webmagic.utils.FilePersistentBase;
import us.codecraft.webmagic.utils.JSONUtils;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Store results in files.<br>
 *
 * @author code4crafter@gmail.com <br>
 * @since 0.1.0
 */
public class FilePipeline extends FilePersistentBase implements Pipeline {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * create a FilePipeline with default path"/data/webmagic/"
     */
    public FilePipeline() {
        setPath("./news/");
    }

    public FilePipeline(String path) {
        setPath(path);
    }

    @Override
    public void process(BaseDO baseDO, Task task) {
        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR;
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + baseDO.getHost() + ".txt", true), "UTF-8"));
            bufferedWriter.write(JSONUtils.objectToStringNonEx(baseDO));
        } catch (IOException e) {
            logger.error("write file error {}", e);
        }finally {
            try {
                bufferedWriter.flush();
                bufferedWriter.close();
            }catch (Exception e) {
                logger.error("close bufferedWriter error {}", e);
            }
        }
    }
}
